/*
 * @lc app=leetcode.cn id=207 lang=javascript
 *
 * [207] 课程表
 */

// @lc code=start
/**
 * @param {number} numCourses
 * @param {number[][]} prerequisites
 * @return {boolean}
 */
var canFinish = function(numCourses, prerequisites) {
    let inDegree=new Array(numCourses).fill(0)
    let map={}
    for(let i=0;i<prerequisites.length;i++){
        inDegree[prerequisites[i][0]]++
        if(map[prerequisites[i][1]]){
            map[prerequisites[i][1]].push(prerequisites[i][0])
        }else{
            map[prerequisites[i][1]]=[prerequisites[i][0]]
        }
    }
    let queue=[]
    for(let i=0;i<inDegree.length;i++){
        if(inDegree[i]==0){
            queue.push(i)
        }
    }
    let count=0
    while(queue.length){
        let head=queue.pop()
        count++
        let currList=map[head]
        for(let i=0;currList&&i<currList.length;i++){
            inDegree[currList[i]]--
            if(inDegree[currList[i]]==0){
                queue.push(currList[i])
            }
        }
    }
    return count===numCourses
};
// @lc code=end

